const User = require('../model/user.model')

class UserService {

  async getUerInfo({
    user_name,
    is_admin,
    id
  }){

    const whereOpt = {}

    id && Object.assign(whereOpt,{id})
    is_admin && Object.assign(whereOpt,{is_admin})
    user_name && Object.assign(whereOpt,{user_name})

    const res = await User.findOne({
      attributes:['id','user_name','password','is_admin'],
      where:whereOpt
    })
 
    return res? res.dataValues:null

  }

  async createUser(user_name,password){

  const createres =   await User.create({user_name,password})
 
  return createres
 
   }

   async updateById({
    user_name,
    is_admin,
    id,
    password
  }){
    const whereOpt = {id};
    const newUser = {}

    user_name && Object.assign(newUser,{user_name})
    is_admin && Object.assign(newUser,{is_admin})
    password && Object.assign(newUser,{password})

    const res = await User.update(newUser,{where:whereOpt})
    
    return res[0] > 0 ? true : false
  }
}
module.exports = new UserService();